package com.tuniu.agents.agent;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.ai.chat.prompt.ChatOptions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * Agent configuration
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class AgentOptions {

    private static final int ChatMemorySize = 20;

    public AgentOptions(String agentId, String promptTemplateId) {
        this(agentId, promptTemplateId, List.of());
    }

    public AgentOptions(String agentId, String promptTemplateId, ChatOptions chatOptions) {
        this(agentId, promptTemplateId, List.of(), Map.of(), chatOptions);
    }

    public AgentOptions(String agentId, String promptTemplateId, List<String> tools) {
        this.agentId = agentId;
        this.promptTemplateId = promptTemplateId;
        this.tools = tools;
        this.advisorParam = new ConcurrentHashMap<>();
        this.advisorParam.put(CHAT_MEMORY_RETRIEVE_SIZE_KEY, ChatMemorySize);
    }

    public AgentOptions(String agentId, String promptTemplateId, List<String> tools, Map<String, Object> advisorParam, ChatOptions chatOptions) {
        this.agentId = agentId;
        this.promptTemplateId = promptTemplateId;
        this.tools = tools;
        this.advisorParam = new HashMap<>(advisorParam != null ? advisorParam : new HashMap<>());
        if (this.advisorParam.get(CHAT_MEMORY_RETRIEVE_SIZE_KEY) == null) {
            this.advisorParam.put(CHAT_MEMORY_RETRIEVE_SIZE_KEY, ChatMemorySize);
        }
        this.chatOptions = chatOptions;
    }

    /**
     * Agent ID
     */
    String agentId;

    /**
     * Agent name
     */
    String agentName;

    /**
     * Prompt template ID
     */
    String promptTemplateId;

    /**
     * Chat model configuration
     */
    ChatOptions chatOptions;

    /**
     * List of tools
     */
    List<String> tools;

    /**
     * Advisor parameters
     */
    Map<String, Object> advisorParam;

}
